/**
 * Coryright 2015-2020 www.eyaomall.cn
 * All rights reserved.
 * @author yuhuafeng
 * @date 2016年9月29日
 *
 */

package com.eyaomall.wxmall.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.eyaomall.wxmall.common.error.BECode;
import com.eyaomall.wxmall.model.po.Order;
import com.eyaomall.wxmall.service.OrderService;
import com.lycheeframework.core.cmp.api.http.GsnResponse;
import com.lycheeframework.core.exception.ErrorCodeException;

/**
 * 定时任务回调接口
 * @author yuhuafeng
 * @date 2016-09-29
 */
@Controller
@RequestMapping("schedulercallback")
public class SchedulerCallBackController {

	@Autowired
	private OrderService orderService;
	
	/**
	 * 关闭订单
	 * @param orderId
	 */
	@RequestMapping("close_order")
	@ResponseBody
	public void closeOrder(Integer orderId){
		if (orderId == null || orderId <= 0)
			throw new ErrorCodeException(BECode.order_4000001);

		Order order = orderService.queryById(orderId);
		if (order == null)
			throw new ErrorCodeException(BECode.order_4000001);

		if (!"0".equalsIgnoreCase(order.getStatus()))
			throw new ErrorCodeException(BECode.order_4000004);
		// 如果是未支付取消订单，状态为关闭，如果已支付订单状态为待退款
		order.setStatus("4");
		orderService.update(order);
	}
	
	/**
	 * 确认订单
	 * @param orderId
	 */
	@RequestMapping("received_order")
	@ResponseBody
	public void receivedOrder(Integer orderId){
		if (orderId == null || orderId <= 0)
			throw new ErrorCodeException(BECode.order_4000001);

		Order order = orderService.queryById(orderId);
		if (order == null)
			throw new ErrorCodeException(BECode.order_4000001);

		if (!"2".equalsIgnoreCase(order.getStatus()))
			throw new ErrorCodeException(BECode.order_4000004);
		orderService.setReceive(order);
	}
}
